package com.lks.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.lks.common.util.PageInfoUtil;
import com.lks.system.dao.IACateDao;
import com.lks.system.dao.IARegionDao;
import com.lks.system.pojo.ACate;
import com.lks.system.pojo.ARegion;
import com.lks.system.service.ISystemService;

@Service("systemService")
public class SystemServiceImpl implements ISystemService
{
	@Resource
	private IARegionDao regionDao;

	@Resource
	private IACateDao cateDao;
	

	@Override
	public ARegion findOneRegionService(Map<String, Object> condMap)
	{
		return this.regionDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneRegionService(ARegion region)
	{
		JSONObject resultJSON = new JSONObject();
		int insert = this.regionDao.insert(region);
		if (insert > 0)
		{
			resultJSON.put("info", "管理员信息添加成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", region.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "管理员信息添加失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRegionService(ARegion region)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.regionDao.update(region);
		if (update > 0)
		{
			resultJSON.put("info", "管理员信息更新成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", region.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "管理员信息更新失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneRegionService(ARegion region)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.regionDao.insert(region);
		if (update > 0)
		{
			resultJSON.put("info", "管理员信息删除成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", region.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "管理员信息删除失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public List<ARegion> findCondListRegionService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<ARegion> regionList = new ArrayList<ARegion>();
		/**
		 * 处理条件
		 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/*
		 * pageCount == true:拿总记录数: pageCount == false:分页数据: pageCount
		 * 为null不分页查询
		 */
		if (pageInfoUtil != null)
		{
			System.out.println("-----分页执行-----");
			// 先分页，1.获取总记录数（pageCount == true） 2.获取分页的查询
			condMap.put("pageCount", "true");
			// 查询总记录数
			regionList = this.regionDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(regionList.get(0).getId());

			/* 获取分页数据 */
			condMap.put("pageCount", "false");
			/* 分页信息 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			// 查询的数据
			regionList = this.regionDao.findCondList(condMap);
		} else
		{
			// 不分页pageCount为null不分页
			regionList = this.regionDao.findCondList(condMap);
		}
		return regionList;
	}

	/*----------------------------------分类表开始--------------------------------*/
	@Override
	public ACate findOneCateService(Map<String, Object> condMap)
	{
		return this.cateDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneCateService(ACate cate)
	{
		JSONObject resultJSON = new JSONObject();
		int insert = this.cateDao.insert(cate);
		if (insert > 0)
		{
			resultJSON.put("info", "管理员信息添加成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", cate.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "管理员信息添加失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneCateService(ACate cate)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.cateDao.update(cate);
		if (update > 0)
		{
			resultJSON.put("info", "管理员信息更新成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", cate.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "管理员信息更新失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneCateService(ACate cate)
	{
		JSONObject resultJSON = new JSONObject();
		int update = this.cateDao.insert(cate);
		if (update > 0)
		{
			resultJSON.put("info", "管理员信息删除成功");
			resultJSON.put("code", "1");

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", cate.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("info", "管理员信息删除失败");
			resultJSON.put("code", "0");
		}
		return resultJSON;
	}

	@Override
	public List<ACate> findCondListCateService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<ACate> cateList = new ArrayList<ACate>();
		/**
		 * 处理条件
		 */
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}

		/*
		 * pageCount == true:拿总记录数: pageCount == false:分页数据: pageCount
		 * 为null不分页查询
		 */
		if (pageInfoUtil != null)
		{
			System.out.println("-----分页执行-----");
			// 先分页，1.获取总记录数（pageCount == true） 2.获取分页的查询
			condMap.put("pageCount", "true");
			// 查询总记录数
			cateList = this.cateDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(cateList.get(0).getId());

			/* 获取分页数据 */
			condMap.put("pageCount", "false");
			/* 分页信息 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			// 查询的数据
			cateList = this.cateDao.findCondList(condMap);
		} else
		{
			// 不分页pageCount为null不分页
			cateList = this.cateDao.findCondList(condMap);
		}
		return cateList;
	}
	/*----------------------------------分类表结束--------------------------------*/

}
